package com.docman.dao;
import java.sql.*;
import java.util.*;

import com.docman.dbconnect.*;
import com.docman.domainmodel.DepartmentBean;
import com.docman.domainmodel.UniversityBean;
import com.docman.domainmodel.UserBean;
public class UserDAO {

static Connection currentCon = null;
static ResultSet rs = null;


public static void register(UserBean user) throws com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException{
	
	 
	
	PreparedStatement stmt = null;
	
	String userName = user.getUserName();
	String firstName=user.getFirstName();
	String lastName = user.getLastName();
	String password= user.getPassword();
	String email = user.getEmail();
	String question = user.getQuestion();
	String answer = user.getAnswer();
	String university = user.getUniversity().getUniversityName();
	String department = user.getDepartment().getDepartmentName();	
	
	String query = "INSERT INTO USERS(userName, firstName, lastName, password, email, question, answer,university, department ,totalRating ,ratingCount) " +
			"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,?)";
	try{
		
		currentCon =ConnectionManager.getInstance().getConnection();
		stmt = currentCon.prepareStatement(query);
		stmt.setString(1, userName);
		stmt.setString(2, firstName);
		stmt.setString(3, lastName);
		stmt.setString(4, password);
		stmt.setString(5, email);
		stmt.setString(6, question);
		stmt.setString(7, answer);
		stmt.setString(8, university);
		stmt.setString(9, department);
		stmt.setInt(10, 0);
		stmt.setInt(11,0);
		//System.out.println("from DAO"+ userName + firstName + lastName+ password);
		/*int check =*/
		stmt.executeUpdate();
		
		/*if(check==0)
			System.out.println("Succesfully inserted user");
		*/
		
		stmt.close();
	
	}
	catch(Exception e){
		
		System.out.println("Register Failed :"+e);
		throw new com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException("Geçersiz kullanıcı adı veya mail adresi, lütfen tekrar deneyiniz.");
	}
	
}


public static UserBean login(UserBean user){
	
	PreparedStatement stmt = null;
	
	String userName= user.getUserName();
	String password=user.getPassword();
	
	
	String query = "select * from USERS where userName=? and password=?";  
	try
	{
		ConnectionManager mgr = ConnectionManager.getInstance();
		currentCon = mgr.getConnection();

	
		stmt = currentCon.prepareStatement(query);  
		stmt.setString(1, userName); 
		stmt.setString(2, password);
	
		rs = stmt.executeQuery(); 
	
		if(!rs.next()){
		
			System.out.println("Not a registered user.");
			user.setValid(false);
	
		}
		else{
			//Successfully login
			String firstName = rs.getString("firstName");
			String lastName = rs.getString("lastName");
			String username = rs.getString("userName");
			String email = rs.getString("email");
			String question = rs.getString("question");
			String answer = rs.getString("answer");
			String university = rs.getString("university");
			String department = rs.getString("department");
			int totalRating = rs.getInt("totalRating");
			int ratingCount = rs.getInt("ratingCount");
			
			System.out.println(firstName+" "+lastName);
			user.setUserName(username);
			user.setFirstName(firstName);
			user.setLastName(lastName);
			user.setEmail(email);
			user.setQuestion(question);
			user.setAnswer(answer);
			user.setRatingCount(ratingCount);
			user.setTotalRating(totalRating);
			UniversityBean uv = new UniversityBean();
			uv.setUniversityName(university);
			user.setUniversity(uv);
			
			DepartmentBean dept = new DepartmentBean();
			dept.setDepartmentName(department);
			user.setDepartment(dept);
			
			user.setValid(true);
		}
	
		rs.close();
		stmt.close();
		}
	
		catch(Exception e)
		{
			System.out.println("Login Failed"+e);
			
		}
		return user;
	}

public static void changePassword(UserBean user) throws Exception{
	
	PreparedStatement stmt = null;
	
	String userName = user.getUserName();
	String oldPass = user.getPassword();
	String newPassword = user.getNewPassword();
	
	String query = "UPDATE USERS SET password =? where userName=? and password=?";
	
	
	
	try{
		currentCon =ConnectionManager.getInstance().getConnection();
		
		stmt = currentCon.prepareStatement(query); 
		stmt.setString(1, newPassword);
		stmt.setString(2, userName);
		stmt.setString(3, oldPass);
		
		int x =stmt.executeUpdate(); 
		
		if(x != 1) {
			throw new Exception("Şifreyi yanlış girdiniz. Lütfen tekrar deneyin.");
		}
		
		stmt.close();
	}
	catch(Exception e){
		
		System.out.println("unable to change password"+e);
		throw new Exception("Şifreyi yanlış girdiniz. Lütfen tekrar deneyin.");
	}
		
}
//if successfully rated method returns row count which is one , otherwise method returns 0.	
public static int rateUser(String userName , int rating){
	int ret = 0;
	String statement = "UPDATE USERS SET ratingCount = ratingCount+1 , totalRating = totalRating+"+rating+" WHERE userName ="+ "\'"+userName.trim() +"\'";
	try{
		currentCon =ConnectionManager.getInstance().getConnection();
		Statement stmt =currentCon.createStatement();
		ret =stmt.executeUpdate(statement);
		
	}
	
	catch(Exception e){
		System.out.println("Cannot rate user : "+ e);
	}
	
	return ret;
	
}

}
